// pages/recipe/recipe.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeDay: new Date().getDay() - 1, // 默认显示今天的菜单
    days: [
      { id: 0, name: '周一' },
      { id: 1, name: '周二' },
      { id: 2, name: '周三' },
      { id: 3, name: '周四' },
      { id: 4, name: '周五' },
      { id: 5, name: '周六' },
      { id: 6, name: '周日' },
    ],
    mealsMapper:[
      {key:'breakfast', name: "早餐"},
      {key:'lunch', name: "午餐"},
      {key:'dinner', name: "晚餐"}
    ],
    meals: {
      0: { breakfast: [], lunch: [], dinner: [] },
      1: { breakfast: [], lunch: [], dinner: [] },
      2: { breakfast: [], lunch: [], dinner: [] },
      3: { breakfast: [], lunch: [], dinner: [] },
      4: {
        breakfast: [
          { name: '油条', image: '/assets/images/recipe/laozhixia.png' }
        ],
        lunch: [
          { name: '炒饭', image: '/assets/images/recipe/laozhixia.png' }
        ],
        dinner: [
          { name: '饺子', image: '/assets/images/recipe/laozhixia.png' }
        ]
       },
      5: {
        breakfast: [
          { name: '豆浆', image: '/assets/images/recipe/laozhixia.png' },
          { name: '油条', image: '/assets/images/recipe/laozhixia.png' }
        ],
        lunch: [
          { name: '炒饭', image: '/assets/images/recipe/laozhixia.png' }
        ],
        dinner: [
          { name: '饺子', image: '/assets/images/recipe/laozhixia.png' }
        ]
      },
      6: { breakfast: [], lunch: [], dinner: [] }
    },
  },
  // 切换星期
  onDayChange(event: any) {
    const activeDay = event.detail;
    console.log(new Date().getDay() - 1, activeDay)
    this.setData({ activeDay });
    this.fetchMeals();
    // Fetch or update meals data based on the selected day
  },
  // 获取周菜单数据
  fetchMeals() {
    wx.request({
      url: 'http://localhost:3000/meals', // 根据你的json-server地址
      method: 'GET',
      success: (res) => {
        console.log({res})
        this.setData({ meals: res.data });
      },
      fail: (err) => {
        console.error('请求失败', err);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // this.setData({
    //   activeDay: this.data.activeDay === 0 ? 6 : this.data.activeDay - 1, // 将星期天的索引调整为 6
    // });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})